import unittest
from unittest.mock import patch, MagicMock
import click.testing
from tools.main import entrance, main
import os

class TestMainModule(unittest.TestCase):
    """测试主入口模块"""
    
    def setUp(self):
        self.runner = click.testing.CliRunner()
    
    @patch.dict(os.environ, {'SPLITTER_SLICE_REPO': 'https://gitee.com/openeuler/slice-releases.git'})
    def test_environment_variable_setup(self):
        """测试环境变量设置"""
        # 验证环境变量是否正确设置
        self.assertEqual(os.environ.get('SPLITTER_SLICE_REPO'), 
                        'https://gitee.com/openeuler/slice-releases.git')
    
    def test_entrance_help(self):
        """测试主入口帮助信息"""
        result = self.runner.invoke(entrance, ['--help'])
        self.assertEqual(result.exit_code, 0)
        self.assertIn('You can set the environment SPLITTER_SLICE_REPO', result.output)
    
    @patch('tools.main.entrance')
    def test_main_function(self, mock_entrance):
        """测试main函数调用"""
        main()
        mock_entrance.assert_called_once()
    
    def test_command_registration(self):
        """测试命令注册"""
        result = self.runner.invoke(entrance, ['--help'])
        self.assertIn('cut', result.output)
        self.assertIn('gen', result.output)
    
    def test_empty_args(self):
        """测试无参数调用"""
        result = self.runner.invoke(entrance, [])
        self.assertEqual(result.exit_code, 0)